package xyz.syyrjx.utils;
import com.alibaba.fastjson.JSON;
import org.springframework.data.redis.core.RedisTemplate;
import xyz.syyrjx.entity.ResultInfo;
import xyz.syyrjx.entity.User;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;


/**
 * 检验Token是否存在
 * @author Mr.Wan
 * @date 2022/10/11 - 19:40
 */
public class TokenUtil {

    public static ResultInfo check(HttpServletRequest request, RedisTemplate redisTemplate) {
        String token = null;
        try {
            token = request.getHeader("Authorization");
            if(redisTemplate.opsForValue().get(token)!=null){
                return ResultInfo.success(redisTemplate.opsForValue().get(token));
            }
        } catch (Exception e) {

        }
        return ResultInfo.fail("token已失效！请重新登录！");
    }

    public static String getUser(HttpServletRequest request){

        String token = null;
        token = request.getHeader("Authorization");
        HashMap hashMap = JSON.parseObject(token, HashMap.class);
        token = (String) hashMap.get("token");
        return token;
    }
}
